package com.beaven.imclient.fragment;

import android.app.ProgressDialog;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.EditText;
import com.beaven.imclient.R;
import com.beaven.imclient.activity.MainActivity;
import com.beaven.imclient.base.BaseFragment;
import com.beaven.imclient.presenter.group.GroupContract;
import com.beaven.imclient.presenter.group.GroupPresenter;
import com.beaven.imclient.utils.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class GroupFragment extends BaseFragment implements GroupContract.View {

  public static GroupFragment newInstance() {
    return new GroupFragment();
  }

  @Override protected int getLayoutId() {
    return R.layout.fragment_group;
  }

  private EditText edit_group_name;
  private ProgressDialog dialog;
  private GroupContract.Presenter presenter;

  @Override protected void initView(View rootView) {
    super.initView(rootView);
    edit_group_name = (EditText) rootView.findViewById(R.id.edit_group_name);
    rootView.findViewById(R.id.bt_create).setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        presenter.createGroup();
      }
    });
  }

  @Override protected void initData() {
    super.initData();
    setPresenter(new GroupPresenter(this));
  }

  @Override public void setPresenter(GroupContract.Presenter presenter) {
    this.presenter = presenter;
  }

  @Override public String getGroupName() {
    return edit_group_name.getText().toString().trim();
  }

  @Override public void showLoading() {
    dialog = ProgressDialog.show(getActivity(), "", getString(R.string.load_now));
  }

  @Override public void hideLoading() {
    if (dialog != null) {
      dialog.dismiss();
    }
  }

  @Override public void showText(int strId) {
    ToastUtil.show(strId);
  }

  @Override public void start() {
    MainActivity.start(getContext());
  }
}
